<?php
class Publication extends Controller
{
	function Publication()
	{
	 	parent::Controller();
		$this->load->model('publication_model');
	}
	
	function index()
	{
		$where = NULL;
		$data['search'] = 'title or publisher';
                $data['area'] ='';
		if($this->input->get('Search') || $this->input->get('thematicArea'))
		{
			$data['search'] = $search = $this->input->get('Search');
			$data['area'] = $area_id = $this->input->get('thematicArea');
			
			$con = '';
			if(is_numeric($area_id))
			{
				$where = '(category_id = '.$area_id.' or sub_category_id = '.$area_id.')';
				$con = ' and ';
			}
			if($search != 'title or publisher')
			{
				$where.= $con.'(title like "%'.$search.'%" or publisher like "%'.$search.'%")';	
			}
			
		}
			$data['total_publications'] = $this->publication_model->total_publications($where);
			create_pagination($data['total_publications'], 3, config_item('per_page'));
			$data['publications'] = $this->publication_model->get_publications(config_item('per_page'), $this->uri->segment(3), $where);
//echo "HERE_". $this->db->last_query();
		//debug_array($data);die();
		$data['page_title']	= 'Manage Publications';
		$data['page'] = 'publication_list_v';
		
		$data['categories'] = $this->general_db_model->getAll('tbl_publication_category', 'category_name', 'parent_id = 0');
		//debug_array($data['categories']);die();
		$this->load->view('main', $data);		
	}
	
	function add()
	{
		if($this->input->post('submit_publication'))
		{
			$this->add_edit();	
		}
		$data['categories'] = $this->general_db_model->getAll('tbl_publication_category', 'category_name', 'parent_id = 0');
		$data['locations'] = $this->general_db_model->getAll('tbl_locations', 'location_name');
	 	$data['page_title']	= 'Add Publication';
		$data['page'] = 'publication_add_v';
		$this->load->view('main', $data);		
	}
	
	function edit($id=NULL)
	{
		if($this->input->post('submit_publication'))
		{
			$this->add_edit($id);	
		}	
		$data['publication'] = $this->publication_model->get_publication_by_id($id);
		$data['categories'] = $this->general_db_model->getAll('tbl_publication_category', 'category_name', 'parent_id = 0');
		$data['locations'] = $this->general_db_model->getAll('tbl_locations', 'location_name');
	 	$data['page_title']	= 'Update Publication';
		$data['page'] = 'publication_add_v';
		$this->load->view('main', $data);		
	}
	
	function add_edit($id=NULL)
	{
            //$this->load->helper('upload');
		//debug_array($_POST);die();
		$data1['title']  = $this->input->post('title');
		$cat_id = $data1['category_id'] = $this->input->post('category_id');
		$sub_cat_id = $data1['sub_category_id'] = $this->input->post('sub_category_id');
		$data1['coding']=$this->input->post('coding');
                //debug_array($_POST);exit;
		/*// generating coding for publication  ////////////////////////////////////
		if($id && $cat_id == $this->input->post('prev_category') && $sub_cat_id == $this->input->post('prev_subcategory'))
		{
			//do nothing
		}
		else
		{
			$last_code = $this->publication_model->get_last_code($cat_id, $sub_cat_id);
			
			$code_arr = explode('-', $last_code);
			
			$code_abbr = $code_arr[0];
					
			if(array_key_exists('1', $code_arr))
			{
				$code_inc = $code_arr[1];
				$code_inc = $code_inc + 1;
				$code_inc = sprintf('%04d', $code_inc);
				$data1['coding'] = $code_abbr.'-'.$code_inc;
			}
			else
				$data1['coding'] = $code_abbr.'-'.'0001';
		}
		//////////////////////////////////////////////////////////////////////////
		*/
		
		$data1['publisher'] = $this->input->post('publisher');
		$data1['pages'] = $this->input->post('pages');
		$data1['publish_year'] = $this->input->post('publish_year');
		$data1['isbn_number'] = $this->input->post('isbn_number');
		$data1['abstract'] = $this->input->post('abstract');
		$data1['remark'] = $this->input->post('remark');
		$data1['location'] = $this->input->post('location');
		$data1['country'] = $this->input->post('country');
		$data1['soft_copy_type'] = $this->input->post('soft_copy_type');
                if($data1['soft_copy_type']=="url"){
                    $data1['soft_copy']=$this->input->post('soft_copy');
                }else{
                    if($_FILES['soft_copy']['name'])
                    {
                            $prev_softcopy = $this->input->post('prev_soft_copy');
                            $soft_copy = upload_file('soft_copy', config_item('publication_softcopy_root'), 'doc|pdf|docx',$prev_softcopy);
                            if($soft_copy)
                                     $data1['soft_copy'] = $soft_copy;
                            else
                                     $data1['soft_copy'] = $prev_softcopy;
                    }
                }
                if($_FILES['image']['name'])
		{
			$prev_image = $this->input->post('prev_image');
			$image = upload_image('image', $this->config->item('publication_images_root'), array('dest' => $this->config->item('publication_thumb_images_root'), 'size' => array('w' => 60, 'h' => 60), 'ratio' => true), $prev_image);
			if($image)
				$data1['image'] = $image;
			else
				$data['image'] = $prev_image;
		}
        
		if($id)
		{
			$this->general_db_model->update('tbl_publication_info', $data1, 'id = '.$id);
			$publication_id = $id;
		}
		else
		{
			$publication_id = $this->general_db_model->insert('tbl_publication_info', $data1);
		}
		//echo $this->db->last_query();die();
		$data2['publication_id'] = $publication_id;
		$data2['type'] = 'en';
		$data2['total_qty_distribution'] = $this->input->post('qty_distribution_en');
		$data2['total_qty_box'] = $this->input->post('qty_box_en');
		$data2['total_qty_store'] = $this->input->post('qty_store_en');
		$data2['total_qty'] = $this->input->post('total_qty_en');
		//$data2['total_qty'] = (int)$data2['total_qty_distribution'] + (int)$data2['total_qty_box'] + (int)$data2['total_qty_store'];
		
		if($id)
			$this->general_db_model->update('tbl_publication_quantity', $data2, 'publication_id = '.$id.' and type = "en"');
		else
			$this->general_db_model->insert('tbl_publication_quantity', $data2);
		
		$data3['publication_id'] = $publication_id;
		$data3['type'] = 'np';
		$data3['total_qty_distribution'] = $this->input->post('qty_distribution_np');
		$data3['total_qty_box'] = $this->input->post('qty_box_np');
		$data3['total_qty_store'] = $this->input->post('qty_store_np');
		//$data3['total_qty'] = (int)$data3['total_qty_distribution'] + (int)$data3['total_qty_box'] + (int)$data3['total_qty_store'];
		$data3['total_qty'] = $this->input->post('total_qty_np');
		
		if($id)
			$this->general_db_model->update('tbl_publication_quantity', $data3, 'publication_id = '.$id.' and type = "np"');
		else
			$this->general_db_model->insert('tbl_publication_quantity', $data3);		
		
		
		if($id)
		{
			$this->session->set_flashdata('success_message', 'Publication - '.$data1['title'].' :: Successfully updated.');
			redirect(filter_url('edit'));
		}
		else
		{
			$this->session->set_flashdata('success_message', 'Publication - '.$data1['title'].' :: Successfully added.');
			redirect(site_url('publication'));
		}
		exit();
	}
	
	function delete($id)
	{
		$image = $this->publication_model->get_publication_by_id($id, 'image');
		$soft_copy = $this->publication_model->get_publication_by_id($id, 'soft_copy');
		
		delete_file(config_item('publication_images_root').$image);
		delete_file(config_item('publication_thumb_images_root').$image);
		delete_file(config_item('publication_softcopy_root').$soft_copy);
		
		$this->general_db_model->delete('tbl_publication_info', 'id = '.$id);
		$this->general_db_model->delete('tbl_publication_quantity', 'publication_id = '.$id);
		redirect(filter_url('delete'));
	}
	
	function ajax_sub_categories()
	{
		$category_id = $this->input->post('cat_id');	
		echo sub_category($category_id);
	}
	
	function details($id)
	{
            $this->load->model('borrower_model');
            $data['publication'] = $this->publication_model->get_publication_detail($id);

            $data['publicationBorrowerList']=$this->borrower_model->get_active_borrower_list($id);
            $data['page_title']	= 'Publication Details';
            $data['page'] = 'publication_detail_v';
            $this->load->view('main', $data);
	}
	 function ajax_generate_code(){
             $cat_id = $this->input->post('cat_id');
             $sub_cat_id = $this->input->post('sub_cat_id');
            $last_code = $this->publication_model->get_last_code($cat_id, $sub_cat_id);
            $code_arr = explode('-', $last_code);
            $code_abbr = $code_arr[0];
            if(array_key_exists('1', $code_arr))
            {
                    $code_inc = $code_arr[1];
                    $code_inc = $code_inc + 1;
                    $code_inc = sprintf('%04d', $code_inc);
                    $coding = $code_abbr.'-'.$code_inc;
            }
            else $coding = $code_abbr.'-'.'0001';
            echo $coding;exit;
        }
        function check_title(){
            $keyword = $this->input->post('title');
            $ckTitle = $this->publication_model->check_title($keyword);
            echo (count($ckTitle)>0)?"false":"true";
        }
	
}

?>